package ms.unicorn.diagram.part;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ms.unicorn.diagram.providers.UnicornElementTypes;
import ms.unicorn.modellanguage.LanguageRegistry;
import ms.unicorn.palette.PaletteGroup;
import ms.unicorn.palette.PaletteItem;
import ms.unicorn.palette.PaletteTool;
import ms.unicorn.util.ErrorHandler;
import ms.unicorn.util.MLExtensionSchema;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.internal.adaptor.PluginParser;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gmf.runtime.notation.Diagram;

/**
 * @generated NOT
 */
public class UnicornPaletteFactory implements MLExtensionSchema {

//	/**
//	 * @generated
//	 */
//	public void fillPalette(PaletteRoot paletteRoot) {
//		paletteRoot.add(createUnicorn1Group());
//	}
//
//	/**
//	 * Creates "unicorn" palette tool group
//	 * @generated
//	 */
//	private PaletteContainer createUnicorn1Group() {
//		PaletteDrawer paletteContainer = new PaletteDrawer(
//				Messages.Unicorn1Group_title);
//		paletteContainer.setId("createUnicorn1Group"); //$NON-NLS-1$
//		paletteContainer.setDescription(Messages.Unicorn1Group_desc);
//		paletteContainer.add(createEntity1CreationTool());
//		paletteContainer.add(createRelation2CreationTool());
//		return paletteContainer;
//	}
//
//	/**
//	 * @generated
//	 */
//	private ToolEntry createEntity1CreationTool() {
//		NodeToolEntry entry = new NodeToolEntry(
//				Messages.Entity1CreationTool_title,
//				Messages.Entity1CreationTool_desc,
//				Collections.singletonList(UnicornElementTypes.Entity_2001));
//		entry.setId("createEntity1CreationTool"); //$NON-NLS-1$
//		entry.setSmallIcon(UnicornElementTypes
//				.getImageDescriptor(UnicornElementTypes.Entity_2001));
//		entry.setLargeIcon(entry.getSmallIcon());
//		return entry;
//	}
//
//	/**
//	 * @generated
//	 */
//	private ToolEntry createRelation2CreationTool() {
//		LinkToolEntry entry = new LinkToolEntry(
//				Messages.Relation2CreationTool_title,
//				Messages.Relation2CreationTool_desc,
//				Collections.singletonList(UnicornElementTypes.Relation_4001));
//		entry.setId("createRelation2CreationTool"); //$NON-NLS-1$
//		entry.setSmallIcon(UnicornElementTypes
//				.getImageDescriptor(UnicornElementTypes.Relation_4001));
//		entry.setLargeIcon(entry.getSmallIcon());
//		return entry;
//	}
//
//	/**
//	 * @generated
//	 */
//	private static class NodeToolEntry extends ToolEntry {
//
//		/**
//		 * @generated
//		 */
//		private final List<IElementType> elementTypes;
//
//		/**
//		 * @generated
//		 */
//		private NodeToolEntry(String title, String description,
//				List<IElementType> elementTypes) {
//			super(title, description, null, null);
//			this.elementTypes = elementTypes;
//		}
//
//		/**
//		 * @generated
//		 */
//		public Tool createTool() {
//			Tool tool = new UnspecifiedTypeCreationTool(elementTypes);
//			tool.setProperties(getToolProperties());
//			return tool;
//		}
//	}
//
//	/**
//	 * @generated
//	 */
//	private static class LinkToolEntry extends ToolEntry {
//
//		/**
//		 * @generated
//		 */
//		private final List<IElementType> relationshipTypes;
//
//		/**
//		 * @generated
//		 */
//		private LinkToolEntry(String title, String description,
//				List<IElementType> relationshipTypes) {
//			super(title, description, null, null);
//			this.relationshipTypes = relationshipTypes;
//		}
//
//		/**
//		 * @generated
//		 */
//		public Tool createTool() {
//			Tool tool = new UnspecifiedTypeConnectionTool(relationshipTypes);
//			tool.setProperties(getToolProperties());
//			return tool;
//		}
//	}

	/**
	 * @param diagramEditor 
	 * @generated NOT
	 */
	public void fillPalette(PaletteRoot paletteRoot, UnicornDiagramEditor diagramEditor) {
		IConfigurationElement[] configs = Platform.getExtensionRegistry()
				.getConfigurationElementsFor(EXTENSION_ID);

		Diagram diagram = diagramEditor.getDiagram();
		ms.unicorn.Diagram domainDiagram = (ms.unicorn.Diagram) diagram.getElement();
		
		// check to filter tool  by domain language
		
		Map<String, PaletteItem> itemMap = new HashMap<String, PaletteItem>();
		List<PaletteGroup> groups = new ArrayList<PaletteGroup>();
		
		createBuiltinItem(itemMap, groups);

		for (IConfigurationElement item : configs) {
			if (PALETTE_CONTRIBUTION.equals(item.getName())) {
				for (IConfigurationElement child : item.getChildren())
					try {
						if (child.getName().equalsIgnoreCase(PALETTE_GROUP))
							PaletteGroup.createGroupFromConfigItem(child,
									groups, itemMap);
						else if (child.getName().equalsIgnoreCase(
								PALETTE_REMOVE)) {
							String id = child.getAttribute("id");
							if (itemMap.containsKey(id))
								itemMap.get(id).setRemoved(true);
						}
					} catch (Exception e) {
						ErrorHandler.handleException(e);
					}
			}
		}

		LanguageRegistry.INSTANCE.registerElementTypes();

		for (PaletteGroup group : groups)
			if (!group.isRemoved())
				paletteRoot.add(group.createPaletteContainer());
	}

	private void createBuiltinItem(Map<String, PaletteItem> maps,
			List<PaletteGroup> groups) {
		//* create unicorn palette tool group
		PaletteGroup unicorn = new PaletteGroup();
		unicorn.setId("default");
		unicorn.setLabel("Default");
		unicorn.setDescription("Default Language Toolsets");
		maps.put(unicorn.getId(), unicorn);
		groups.add(unicorn);

		PaletteTool createEntityTool = new PaletteTool();
		createEntityTool.setId("default.entity");
		createEntityTool.setLanguage("default");
		createEntityTool.setTypeId("default.Entity");
		createEntityTool.setLabel("Entity");
		createEntityTool.setTooltip("Create an entity");
		createEntityTool.setSmallImage(UnicornElementTypes
				.getImageDescriptor(UnicornElementTypes.Entity_2001));
		createEntityTool.setLargeImage(createEntityTool.getSmallImage());

		maps.put(createEntityTool.getId(), createEntityTool);
		unicorn.getTools().add(createEntityTool);

		PaletteTool createRelationTool = new PaletteTool();
		createRelationTool.setId("default.relation");
		createRelationTool.setLanguage("default");
		createRelationTool.setTypeId("default.Relation");
		createRelationTool.setLabel("Relation");
		createRelationTool.setTooltip("Create a relation between two entity");
		createRelationTool.setSmallImage(UnicornElementTypes
				.getImageDescriptor(UnicornElementTypes.Relation_4001));
		createRelationTool.setLargeImage(createEntityTool.getSmallImage());
		maps.put(createRelationTool.getId(), createRelationTool);
		unicorn.getTools().add(createRelationTool);
	}

}
